package com.machine.dao.user.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;



import com.machine.bean.User;
import com.machine.dao.user.UserDao;

@Component(value = "userDao")
@Transactional
public class UserDaoImpl implements UserDao {
	
	/*
	public static void main(String[] args) {
		UserDaoImpl daoImpl = new UserDaoImpl();
		User user = new User();
		user.setUsername("lcq");
		user.setCompany("com");
		user.setRegisterTime("2012");
		user.setRole("haha");
		user.setSex("nan");
		user.setSupplierClass("hehe");
		daoImpl.save(user);
	}
	*/
	
	private SessionFactory sessionfactory;
	
	public SessionFactory getSessionfactory() {
		return sessionfactory;
	}

	@Resource
	public void setSessionfactory(SessionFactory sessionfactory) {
		this.sessionfactory = sessionfactory;
	}

	@Override
	public void deleteUser(User user) {

	}

	@Override
	public User getUserById(int id) {
		String hql = "from User user where user.id ="
			+ id + " ";
		Session session = sessionfactory.getCurrentSession();
		Query query = session.createQuery(hql);
		List<User> userList = query.list();
		if (userList.size() > 0) {
			return userList.get(0);
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public User getUserByName(String username) {
		String hql = "from User user where user.username='" + username +"'";
		Session session = sessionfactory.getCurrentSession();
		
		Query query = session.createQuery(hql);
		List<User> list = query.list();
		if(list.size() > 0){
			System.out.println("has user :" + list.get(0).getUsername());
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<User> getUsers() {
		return null;
	}

	@Override
	public int save(User user) {
		if(getUserByName(user.getUsername()) == null){
			Session session = sessionfactory.getCurrentSession();
			String sql = "insert into user(username,company,password,tel,sex,role,supplierClass,registerTime) values('"+user.getUsername()+"','"+user.getCompany()+"',md5("+user.getPassword()+"),'"+user.getTel()+"','"+user.getSex()+"','"+user.getRole()+"','"+user.getSupplierClass()+"','"+user.getRegisterTime()+"')";
			Query query = session.createSQLQuery(sql);
			query.executeUpdate();
			return 1;
		}else{
			return 0;
		}
	}

	@Override
	public void updateUser(User user) {

	}

	@SuppressWarnings("unchecked")
	@Override
	public User getUser(String username, String password) {
		password = "md5("+password+")";
		String hql = "from User user where user.username='" + username +"' and user.password=" + password +"";
		Session session = sessionfactory.getCurrentSession();
		
		Query query = session.createQuery(hql);
		List<User> list = query.list();
		if(list.size() > 0){
			return list.get(0);
		}
		return null;
	}

	@Override
	public void insertBetterSupport(String componentname, String dingdanhao,
			String gonghuoshang) {
		Session session = sessionfactory.getCurrentSession();
		String sql = "insert into goodgonghuoshang(componentname,dingdanhao,gonghuoshang) values('"+componentname+"','"+dingdanhao+"','"+gonghuoshang+"')";
		Query query = session.createSQLQuery(sql);
		query.executeUpdate();
	}

	@Override
	public void deleteById(String id) {
		Session session = sessionfactory.getCurrentSession();
		String sql = "delete from goodgonghuoshang where id = "+id+"";
		Query query = session.createSQLQuery(sql);
		query.executeUpdate();
	}

	@Override
	public void insertUpload(String path, String name) {
		Session session = sessionfactory.getCurrentSession();
		String sql = "insert into recommended_products(name,pic) values('"+name+"','"+path+"')";
		Query query = session.createSQLQuery(sql);
		query.executeUpdate();
	}

	@Override
	public boolean uploadCheck() {
		Session session = sessionfactory.getCurrentSession();
		String sql = "select count(*) from recommended_products";
		Query query = session.createSQLQuery(sql);
		List list = query.list();
		if(list!=null){
			int num = Integer.valueOf(String.valueOf(list.get(0)));
			if(num <10)
				return true;
		}
		return false;
	}

	@Override
	public void deleteRecomment(String id) {
		Session session = sessionfactory.getCurrentSession();
		String sql = "delete from recommended_products where id = "+id+"";
		System.out.println("hahhaha"+sql);
		Query query = session.createSQLQuery(sql);
		query.executeUpdate();
	}

	@Override
	public void insertads(String pic) {
		Session session = sessionfactory.getCurrentSession();
		String sql = "insert into ads(pic) values('"+pic+"')";
		Query query = session.createSQLQuery(sql);
		query.executeUpdate();
	}

	@Override
	public void deleteAds(String id) {
		Session session = sessionfactory.getCurrentSession();
		String sql = "delete from ads where id = "+id+"";
		Query query = session.createSQLQuery(sql);
		query.executeUpdate();
	}

}
